# = test.photo.13.rb -  Photo Upload Test
# 
# See LICENSE.txt for licensing details.
#
# == Description
#This is to test the upload option for a picture under " My Photo"
require 'watir'
require 'watir/WindowHelper'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'

class TC_PhotoPost_test < Test::Unit::TestCase
    
    def startup			
      $PostSubject = randomChar(20)
      $PostBody = randomChar(50)
      $ImagePath = build_windows_path('resources/gallery_slideshow.gif')
      @@ie = Watir::IE.start($SITE_URL)
    end
  
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end

    def test_01_verifyHomeText
      assert(ie.contains_text(/Community Server/))	
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
      ie.link(:text, 'Sign in').click
      assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
      ie.text_field(:id, /username$/).value = $ADMIN_USER
      assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
      ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
      assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
      ie.link(:id, /loginButton$/).click
      test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
      assert(ie.link(:text, /Control Panel/).exists?)
      ie.link(:text, /Control Panel/).click
      assert(ie.contains_text(/Community Server Announcements/))      
    end
    
    def test_07_verifyControlPanelTabBarLinks
      assert_equal $SITE_URL + 'controlpanel/default.aspx', ie.link(:text, 'Dashboard').href
      assert_equal $SITE_URL + 'controlpanel/blogs', ie.link(:text, 'My Blogs').href
      assert_equal $SITE_URL + 'controlpanel/photos/', ie.link(:text, 'My Photos').href
      assert_equal $SITE_URL + 'controlpanel/files', ie.link(:text, 'My Files').href
      assert_equal $SITE_URL + 'controlpanel/moderation', ie.link(:text, 'Forums Moderation').href
      assert_equal $SITE_URL + 'controlpanel/Setup.aspx', ie.link(:text, 'Administration').href
      assert_equal $SITE_URL + 'controlpanel/Reporting/default.aspx', ie.link(:text, 'Reporting').href
    end
    
    def test_08_navigateVerifyMyPhotosDashboard
      ie.link(:text, 'My Photos').click
      if !(ie.contains_text("My Photos Dashboard"))
        ie.link(:text, 'Sample').click
      end
      
      assert(ie.contains_text("My Photos Dashboard"))
      assert(ie.contains_text("Current Photo Gallery"))   
    end
    
    def test_09_navigateToAddPost
      ie.goto($SITE_URL + 'controlpanel/photos/posteditor.aspx')
      assert(ie.contains_text("Publish New Photo"))
    end
    
    def test_10_verifyLinksExistOnPostEditorPage
      assert(ie.link(:id, /TopLeft_HyperLink1$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon9$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon14$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon17$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon2$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon1$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon8$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon5$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon6$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon7$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon10$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon13$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon11$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon12$/).exists?)
      assert(ie.link(:id, /PostEditControl1_Helpicon16$/).exists?)
      assert(ie.link(:id, /TaskRegion_SaveButton$/).exists?)
    end
    
    def test_11_verifyFieldsExistOnPostEditorPage
      assert(ie.select_list(:id, /PostEditControl1_HourPicker$/).exists?)
      assert(ie.select_list(:id, /PostEditControl1_MinutePicker$/).exists?)
      assert(ie.select_list(:id, /PostEditControl1_AMPM$/).exists?)
      assert(ie.select_list(:id, /PostEditControl1_ModerationDDL$/).exists?)
      assert(ie.select_list(:id, /PostEditControl1_NotificationType$/).exists?)
      assert(ie.radio(:id, /ynPublished_0$/).exists?)
      assert(ie.radio(:id, /ynPublished_1$/).exists?)
      assert(ie.radio(:id, /ynEnableReplies_0$/).exists?)
      assert(ie.radio(:id, /ynEnableReplies_1$/).exists?)
      assert(ie.radio(:id, /ynEnableRatings_0$/).exists?)
      assert(ie.radio(:id, /ynEnableRatings_1$/).exists?)
      assert(ie.radio(:id, /ynEnableTrackbacks_0$/).exists?)
      assert(ie.radio(:id, /ynEnableTrackbacks_1$/).exists?)
      assert(ie.radio(:id, /ynEnableOrderPrints_0$/).exists?)
      assert(ie.radio(:id, /ynEnableOrderPrints_1$/).exists?)
      assert(ie.text_field(:id, /PostEditControl1_ServerFileName$/).exists?)
      assert(ie.text_field(:id, /PostEditControl1_ServerFriendlyFileName$/).exists?)
      assert(ie.text_field(:id, /_Tags$/).exists?)
    end
    
    def test_12_uploadPicture
      assert(ie.button(:text, 'Upload Photo').exists?)
      ie.button(:text, 'Upload Photo').click
      sleep 5
      
      assert(ie.frame(:index, 16).fileField(:id, /AttachmentData$/).exists?)
      assert(ie.frame(:index, 16).link(:text, "Upload").exists?)
      
      puts 'image path:'
      puts $ImagePath
      sleep 10
      ie.frame(:index, 16).fileField(:id, /AttachmentData$/).set($ImagePath)
      
      ie.frame(:index, 16).link(:text, "Upload").click
      sleep 5 
      ie.text_field(:id, /ServerFriendlyFileName$/).value = $PostSubject
      ie.ie.Document.parentWindow.execScript("tinyMCE.getInstanceById(tinyMCE.getEditorId('mce_editor_0')).execCommand('mceSetContent', false, '" + $PostBody + "')")
      
      ie.link(:id, /TaskRegion_SaveButton$/).click
      sleep 5 
    end
    
    def test_13_verifyPictureUploadedSuccessfuly
      assert(ie.contains_text("All Photos"))
      assert(ie.contains_text("Add and edit photos"))      
    end

end